package cn.st4rlight.util.page;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;

import com.google.common.collect.Lists;

import lombok.Data;

/**
 * @author st4rlight <st4rlight@163.com>
 * Created on 2025-05-02
 */
@Data
public class PageWrapper<T> {

    // 当前页面
    private Integer pageNo;
    // 每页显示条数
    private Integer pageSize;
    // 总记录数
    private Integer totalCount;
    // 总页数
    private List<T> dataList = Lists.newArrayList();


    public PageWrapper(Integer pageNo, Integer pageSize) {
        this.pageNo = pageNo;
        this.pageSize = pageSize;
    }

    public PageWrapper(Integer pageNo, Integer pageSize, Integer totalCount) {
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.totalCount = totalCount;
    }

    public PageWrapper(PageRequest pageRequest, Integer totalCount, List<T> dataList) {
        this(pageRequest.getPageNo(), pageRequest.getPageSize(), totalCount);
        this.dataList = dataList;
    }

    public PageWrapper(Integer pageNo, Integer pageSize, Integer totalCount, List<T> dataList) {
        this(pageNo, pageSize, totalCount);
        this.dataList = dataList;
    }

    public PageWrapper(PageWrapper<T> oldPage, List<T> dataList) {
        this(oldPage.getPageNo(), oldPage.getPageSize(), oldPage.getTotalCount());
        this.dataList = dataList;
    }

    public static <T> PageWrapper<T> getDefaultPage(PageRequest pageRequest) {
        return new PageWrapper<>(pageRequest, 0, Lists.newArrayList());
    }

    public static <T> PageWrapper<T> getDefaultPage(int pageNo, int pageSize)  {
        return new PageWrapper<>(pageNo, pageSize, 0, Lists.newArrayList());
    }

    public static <T> PageWrapper<T> emptyPage(PageRequest pageRequest, Integer totalCount)  {
        return new PageWrapper<>(pageRequest, totalCount, Lists.newArrayList());
    }

    public static <T> PageWrapper<T> emptyPage(PageRequest pageRequest)  {
        return new PageWrapper<>(pageRequest, 0, Lists.newArrayList());
    }

    public static <T> PageWrapper<T> fromPage(PageWrapper<T> pageWrapper, List<T> dataList)  {
        return new PageWrapper<>(pageWrapper, dataList);
    }

    public boolean isEmpty() {
        return CollectionUtils.isEmpty(this.dataList);
    }
}
